using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Web;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    /// <summary>
    /// 
    /// </summary>
    public class GeneralSetting
    {
        public long ID = 0;
        public string Name = "";
        public string Code = "";
        public string Value = "";
        public string Description = "";
        public GeneralSetting()
        {
        }
        public GeneralSetting(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            if (ID <= 0) // new object, we insert new record to database
            {
                // build SQL statement
                sqlCommand = "Insert into tbl_GeneralSetting (" +
                             "Name," +
                             "Code," +
                             "Value," +
                             "Description) " +
                         " values(" +
                             "'" + DBModule.RefineString(Name) + "'," +
                             "'" + DBModule.RefineString(Code) + "'," +
                             "'" + DBModule.RefineString(Value) + "'," +
                             "'" + DBModule.RefineString(Description) + "')";
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
                UserObjectsLog.SaveObjectActionLog("GeneralSetting", "add", cn, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                sqlCommand = "Update tbl_GeneralSetting set " +
                             "Name='" + DBModule.RefineString(Name) + "'," +
                             "Code='" + DBModule.RefineString(Code) + "'," +
                             "Value='" + DBModule.RefineString(Value) + "'," +
                             "Description='" + DBModule.RefineString(Description) + "'" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
                UserObjectsLog.SaveObjectActionLog("GeneralSetting", "edit", cn, trans);
            }            
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            //Buil SQL Statement
            //Run SQL Statement
            string sqlCommand = "DELETE FROM tbl_GeneralSetting WHERE ID = " + this.ID.ToString() + " limit 1";
            DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
            UserObjectsLog.SaveObjectActionLog("GeneralSetting", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_GeneralSetting WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                    ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Name"))
                    Name = dr["Name"].ToString();
                if (!dr.IsNull("Code"))
                    Code = dr["Code"].ToString();
                if (!dr.IsNull("Value"))
                    Value = dr["Value"].ToString();
                if (!dr.IsNull("Description"))
                    Description = dr["Description"].ToString();
            }
            else
            {
            }
        }
        public static void GetList(string iDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_GeneralSetting where 1=1 ";
            if ((iDs != "") && (iDs != null))
            {
                sqlCommand += " AND `ID` in (" + iDs + ")";
            }

            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(sqlCommand, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
        public static void DoSearch(string keywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select `ID` from tbl_GeneralSetting where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
        public static int isExisting(long inputID, string inputCode, MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "Select ID from tbl_GeneralSetting where Code='" + DBModule.RefineString(inputCode) + "' AND ID <>" + inputID + " limit 1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
            if (ds.Tables[0].Rows.Count > 0)
                return 1;
            else
                return 0;
        }
        public static string GetValueByCode(string inputCode, MySqlConnection cn, MySqlTransaction trans)
        {
            string retVal = "";
            string sqlCommand = "Select Value from tbl_GeneralSetting where Code='" + DBModule.RefineString(inputCode) + "' limit 1";
            string tempVal = DBModule.ExecuteQueryForOneResult(sqlCommand, cn, trans);
            if (!String.IsNullOrEmpty(tempVal))
                retVal = tempVal;
            return retVal;
        }
    }
}
